package com.zmm.weblog.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmm.weblog.admin.model.vo.category.AddCategoryReqVo;
import com.zmm.weblog.admin.model.vo.category.DeleteCategoryReqVo;
import com.zmm.weblog.admin.model.vo.category.FindCategoryPageListReqVo;
import com.zmm.weblog.admin.model.vo.category.FindCategoryPageListRspVo;
import com.zmm.weblog.admin.service.AdminCategoryService;
import com.zmm.weblog.common.domain.dos.ArticleCategoryRelDo;
import com.zmm.weblog.common.domain.dos.CategoryDo;
import com.zmm.weblog.common.domain.mapper.ArticleCategoryRelMapper;
import com.zmm.weblog.common.domain.mapper.CategoryMapper;
import com.zmm.weblog.common.enums.ResponseCodeEnum;
import com.zmm.weblog.common.exception.BizException;
import com.zmm.weblog.common.model.vo.SelectRspVo;
import com.zmm.weblog.common.utils.PageResponse;
import com.zmm.weblog.common.utils.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author:福禄寿
 * @Date:2024/4/30 下午4:14
 * @Des:weblog-springboot
 **/
@Service
@Slf4j
public class AdminCategoryServiceImpl implements AdminCategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    ArticleCategoryRelMapper articleCategoryRelMapper;

    @Override
    public Response addCategory(AddCategoryReqVo addCategoryReqVo) {
        String categoryName = addCategoryReqVo.getName();

        CategoryDo categoryDo = categoryMapper.selectByName(categoryName);
        if (Objects.nonNull(categoryDo)) {
            log.warn("分类已存在");
            throw new BizException(ResponseCodeEnum.CATEGORY_NAME_IS_EXISTED);
        }
        CategoryDo build = CategoryDo.builder().name(categoryName.trim()).build();

        categoryMapper.insert(build);
        return Response.success();
    }

    @Override
    public PageResponse findCategoryPageList(FindCategoryPageListReqVo findCategoryPageListReqVo) {
        //获取当前页、以及每页需要展示的数据数量
        Long current = findCategoryPageListReqVo.getCurrent();
        Long size = findCategoryPageListReqVo.getSize();
        String name = findCategoryPageListReqVo.getName();
        LocalDate startDate = findCategoryPageListReqVo.getStartDate();
        LocalDate endDate = findCategoryPageListReqVo.getEndDate();

        Page<CategoryDo> categoryDoPage = categoryMapper.selectPageList(current, size, name, startDate, endDate);
        //执行分页查询
        List<CategoryDo> categoryDos = categoryDoPage.getRecords();
        System.out.println("categoryDos =============== " + categoryDos);
        //DO转VO
        List<FindCategoryPageListRspVo> vos = null;
        if (CollectionUtils.isNotEmpty(categoryDos)) {
            vos = categoryDos.stream().map(categoryDo ->
                    FindCategoryPageListRspVo.builder()
                            .id(categoryDo.getId())
                            .name(categoryDo.getName())
                            .createTime(categoryDo.getCreateTime()).build()
            ).collect(Collectors.toList());
        }
        //返回结果
        return PageResponse.success(categoryDoPage, vos);
    }

    @Override
    public Response deleteCategory(DeleteCategoryReqVo deleteCategoryReqVo) {

        ArticleCategoryRelDo categoryRelDO = articleCategoryRelMapper.selectByCategoryId(deleteCategoryReqVo.getId());

        if (Objects.nonNull(categoryRelDO)) {
            log.warn("分类已被使用,不能删除");
            throw new BizException(ResponseCodeEnum.CATEGORY_CAN_NOT_DELETE);
        }

        int count = categoryMapper.deleteById(deleteCategoryReqVo.getId());
        return count > 0 ? Response.success() : Response.fail(ResponseCodeEnum.CATEGORY_NOT_EXISTED);
    }

    @Override
    public Response selectListCategory() {

        //查询所有分类
        List<CategoryDo> categoryDos = categoryMapper.selectList(null);
        //DO转VO
        List<SelectRspVo> vos = null;
        //如果分类数据不为空
        if (!CollectionUtils.isEmpty(categoryDos)) {
            //将分类Id作为Value值，将分类名称作为label展示
            vos = categoryDos.stream().map(categoryDo ->
                    SelectRspVo.builder()
                            .label(categoryDo.getName())
                            .value(categoryDo.getId())
                            .build()
            ).collect(Collectors.toList());
        }
        return Response.success(vos);
    }
}